package org.loofer.uiframework.navigator.backstack;

import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;

public class FragmentActivityBackHelper implements IFragmentBackHelper {
    private FragmentBackStackManager mFragmentBackStackManager;

    public FragmentActivityBackHelper(FragmentActivity activity) {
        this.mFragmentBackStackManager = new FragmentBackStackManager(activity.getSupportFragmentManager());
    }

    public boolean onBackPressed() {
        if (this.mFragmentBackStackManager.isBackStackEmpty()) {
            return false;
        }
        this.mFragmentBackStackManager.getTopFragment().onBackPressed();
        return true;
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return this.mFragmentBackStackManager.getTopFragment() != null && this.mFragmentBackStackManager.getTopFragment().getFragmentBackHelper().onKeyDown(keyCode, event);
    }

    public final void setLaunchFragmentAttr(int containerViewRes, int enterAnimRes, int popExitAnimRes) {
        this.mFragmentBackStackManager.setLaunchFragmentAttr(containerViewRes, enterAnimRes, popExitAnimRes);
    }

    public final void launchFragment(AbstractFragment fragment, int containId, int enterAnimRes, int popExitAnimRes, int launchFlag) {
        fragment.getFragmentBackHelper().setFragmentBackStackManager(this.mFragmentBackStackManager);
        fragment.setLaunchFragmentFlag(launchFlag);
        this.mFragmentBackStackManager.launchFragment(fragment, containId, enterAnimRes, popExitAnimRes);
    }

    public void clearFragmentBackStack() {
        if (this.mFragmentBackStackManager != null) {
            this.mFragmentBackStackManager.clear();
        }
    }

    public final boolean isFragmentBackStackEmpty() {
        return this.mFragmentBackStackManager == null || this.mFragmentBackStackManager.isBackStackEmpty();
    }

    public final void launchFragment(AbstractFragment fragment) {
        fragment.getFragmentBackHelper().setFragmentBackStackManager(this.mFragmentBackStackManager);
        this.mFragmentBackStackManager.launchFragment(fragment);
    }

    public final void finishTopFragment() {
        AbstractFragment topFragment = getTopFragment();
        if (this.mFragmentBackStackManager != null && topFragment != null) {
            this.mFragmentBackStackManager.destroyFragment(topFragment);
        }
    }

    public AbstractFragment getTopFragment() {
        return this.mFragmentBackStackManager != null ? this.mFragmentBackStackManager.getTopFragment() : null;
    }

    public FragmentBackStackManager getFragmentBackStackManager() {
        return this.mFragmentBackStackManager;
    }

    public final void launchRootFragment(AbstractFragment fragment) {
        fragment.getFragmentBackHelper().setFragmentBackStackManager(this.mFragmentBackStackManager);
        this.mFragmentBackStackManager.launchRootFragment(fragment);
    }

    public final AbstractFragment getRootFragment() {
        return this.mFragmentBackStackManager.getRootFragment();
    }

    private void assertFragmentBackStackManager() {
        if (this.mFragmentBackStackManager == null) {
            throw new IllegalArgumentException("you must call setLaunchFragmentAttr(int replaceResId, int enterAnimRes, int popExitAnimRes) first");
        }
    }
}
